package com.neusoft.neuiotms.dcom.controller.incident;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.dto.FileDTO;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.neuiotms.dcom.dto.incident.IncidentHandleDTO;
import com.neusoft.neuiotms.dcom.service.incident.IncidentHandleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:03:59
 */
@Api(tags = "事件处理")
@RestController
@RequestMapping(value = "/api/w/incident/handle")
public class IncidentHandleController {
    @Autowired
    private IncidentHandleService incidentHandleService;
    @Autowired
    private FileService fileService;

    @ApiOperation("新增事件处理")
    @PostMapping
    public ResultDTO<IncidentHandleDTO> create(@RequestBody final IncidentHandleDTO dto, final UserBean user) {
        if (user != null) {
            dto.setExecuteUserCode(StringUtils.isEmpty(user.getUsername()) ? "admin" : user.getUsername());
            dto.setExecuteUserName(StringUtils.isEmpty(user.getName()) ? "admin" : user.getName());
        }
        return ResultDTO.success(this.incidentHandleService.create(dto, user));
    }

    @ApiOperation("查看事件处理")
    @GetMapping("/{workOrderNum}/{taskId}")
    public ResultDTO<IncidentHandleDTO> details(@PathVariable final String workOrderNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.incidentHandleService.detail(workOrderNum, taskId, user));
    }

    @ApiOperation("下载文件")
    @GetMapping("/download/{fsId}")
    public void download(@PathVariable final String fsId, final HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }

    @ApiOperation("上传文件")
    @PostMapping("/upload/attachment")
    public ResultDTO<FileDTO> upload(@RequestParam("file") final MultipartFile file) {
        final FileDTO doc = this.fileService.save(file);
        return ResultDTO.success(doc);
    }

    @ApiOperation("下载事件处理文件")
    @GetMapping("/download/handling/{fsId}")
    public void downloadHandlingAttachment(@PathVariable final String fsId, final HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/delete/attachment/{fsId}")
    public void deleteAttachments(@PathVariable final String fsId) {
        this.incidentHandleService.deleteMongoFile(fsId);
    }

}
